package ying.session01;/**
 * ClassName: GetSession
 * Package: ying.session01
 * Description:
 *
 * @Author 刘英杰
 * @Create 2024/10/5 16:10
 * @Version 1.0
 */

import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.io.IOException;

/**
 * 获取Session
 * HttpServletRequest中的方法:
 *     public HttpSession getSession():
 *          如果当前服务端没有Session,那么就在服务端新建一个Session
 *          如果当前服务端有这个Session,那么就返回这个Session
 * HttpSession中的方法:
 *    public Object getAttribute(String name):从session中根据name取出对应的值
 * */

@WebServlet("/session/get")
public class GetSession extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();

        response.getWriter().write(session.getAttribute("id").toString());
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
